package cn.wolfcode.web.controller;

import cn.wolfcode.domain.CustomerTraceHistory;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ICustomerTraceHistoryService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {
    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"customerTraceHistory:list","班级列表"},logical = Logical.OR)
    public String list(Model model, QueryObject qo){
        PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Employee> employees = employeeService.selectAll();
        model.addAttribute("qo", qo);
        model.addAttribute("employees", employees);
        return "customerTraceHistory/customerTraceHistory";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"customerTraceHistory:delete","班级删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        try {
            customerTraceHistoryService.delete(id);
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }

    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"customerTraceHistory:input","班级编辑"},logical = Logical.OR)
    public String input(Long id,Model model){
        if(id != null){
            CustomerTraceHistory customerTraceHistory = customerTraceHistoryService.selectByPrimaryKey(id);
            model.addAttribute("customerTraceHistory", customerTraceHistory);
        }
        return "customerTraceHistory/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customerTraceHistory:saveOrUpdate","班级新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Model model,@ModelAttribute("qo") CustomerTraceHistory customerTraceHistory,Long id){
        //设置录入人
        customerTraceHistory.setInputUser(UserContext.getCurrentUser());
        customerTraceHistoryService.insert(customerTraceHistory);
        return new JsonResult();
    }
}
